home *** CD-ROM | disk | FTP | other *** search
/ Champak 142 / Volume 142 Oct 17 2011 - Damaged.iso / Games / pocket-rocket.swf / scripts / Key.as < prev    next >
Text File  |  2011-10-17  |  1KB  |  59 lines

  1. package
  2. {
  3.    import flash.display.Stage;
  4.    import flash.events.Event;
  5.    import flash.events.KeyboardEvent;
  6.    
  7.    public class Key
  8.    {
  9.       
  10.       private static var initialized:Boolean = false;
  11.       
  12.       private static var keysDown:Object = new Object();
  13.        
  14.       
  15.       public function Key()
  16.       {
  17.          super();
  18.       }
  19.       
  20.       public static function initialize(param1:Stage) : void
  21.       {
  22.          if(!initialized)
  23.          {
  24.             param1.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
  25.             param1.addEventListener(KeyboardEvent.KEY_UP,keyReleased);
  26.             param1.addEventListener(Event.DEACTIVATE,clearKeys);
  27.             initialized = true;
  28.          }
  29.       }
  30.       
  31.       private static function clearKeys(param1:Event) : void
  32.       {
  33.          keysDown = new Object();
  34.       }
  35.       
  36.       public static function isDown(param1:uint) : Boolean
  37.       {
  38.          if(!initialized)
  39.          {
  40.             throw new Error("Key class has yet been initialized.");
  41.          }
  42.          return Boolean(param1 in keysDown);
  43.       }
  44.       
  45.       private static function keyPressed(param1:KeyboardEvent) : void
  46.       {
  47.          keysDown[param1.keyCode] = true;
  48.       }
  49.       
  50.       private static function keyReleased(param1:KeyboardEvent) : void
  51.       {
  52.          if(param1.keyCode in keysDown)
  53.          {
  54.             delete keysDown[param1.keyCode];
  55.          }
  56.       }
  57.    }
  58. }
  59.